package com.health.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dao.OrderMapper;
import com.health.dao.OrderSettingMapper;
import com.health.pojo.Order;
import com.health.vo.PageResult;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderSettingMapper orderSettingMapper;

    @Override
    public int findCountByCondition(Order order) {
        return orderMapper.findCountByCondition(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(Order order) {
        //添加预约信息
        orderMapper.add(order);

        //已预约人数+1
        int count = orderSettingMapper.updateReservations(order.getOrderDate());
        if (count == 0) {
            throw new RuntimeException("预约失败，请尝试更换其它体检日期");
        }
        return order.getId();
    }

    @Override
    public Map<String, Object> findDetail(Integer id) {
        return orderMapper.findDetail(id);
    }

    @Override
    public Map<String, Object> findOrderReport() {
        return orderMapper.findOrderReport();
    }

    @Override
    public PageResult<Order> findPage(Integer pageNow, Integer pageSize, String query) {
        //使用MyBatisPageHelper插件分页
        PageHelper.startPage(pageNow, pageSize);

        //条件查询数据库,Page中包含了查询到的总行数，以及分页列表数据
        Page<Order> page = orderMapper.findPage(query);

        //获取总行数，分页后的列表数据返回
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public int deleteById(Integer id) {
        return orderMapper.deleteById(id);
    }

    @Override
    public Order findById(Integer id) {
        return orderMapper.findById(id);
    }

    @Override
    public int update(Order order) {
        return orderMapper.update(order);
    }

}
